package com.prs.crm.job.impl;

import java.util.Date;
import java.util.List;

import com.prs.crm.domain.cqunicom.BusinessOrder;
import com.prs.crm.domain.cqunicom.PoplarOrder;
import com.prs.crm.job.ByOrderTask;
import com.prs.crm.service.cqunicom.BusinessOrderService;
import com.prs.crm.service.cqunicom.OrderService;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2011-4-8 下午04:38:39
 * 简单说明
 */

public class ByOrderTaskImpl implements ByOrderTask {
	private  OrderService orderService;
	private BusinessOrder businessOrder;
	private BusinessOrderService businessOrderService;
	
	public BusinessOrder getBusinessOrder() {
		return businessOrder;
	}
	public void setBusinessOrder(BusinessOrder businessOrder) {
		this.businessOrder = businessOrder;
	}
	public BusinessOrderService getBusinessOrderService() {
		return businessOrderService;
	}
	public void setBusinessOrderService(BusinessOrderService businessOrderService) {
		this.businessOrderService = businessOrderService;
	}
	public OrderService getOrderService() {
		return orderService;
	}
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
	public void bysetOrder() {

		// 提醒次数和处理时长设置
		setRemindTime();
		// 自动处理操作
		processOrders();

	}
	/**
	 * 设置提醒次数
	 */
	public void setRemindTime() {
		List<PoplarOrder> orderlist = this.getOrderService().getAllOrderList();
		int siess = orderlist.size();
		if (siess > 0) {

			for (PoplarOrder od : orderlist) {

				int inits = 0;
				if ("1".equals(od.getNewOrderState())) {
					long between = (new Date().getTime() - od.getRecordTime()
							.getTime()) / 1000;// 除以1000是为了转换成秒
					long betweens = (between / 60) / 3;// 提醒次数
					od.setRemindTime(betweens);
					inits++;

				}
				if ("2".equals(od.getProcessSucess())) {
					long seconds = (od.getCompleDate().getTime() - od
							.getAccpectDate().getTime()) / 1000;
					long timeLengths = (seconds / 60) / 60;
					od.setHandleTime(timeLengths + "小时");
					inits++;

				}
				if (inits > 0) {

					this.getOrderService().submit(od);
				}

			}
		}

	}
	/**
	 * 单据自动处理
	 */
	public void processOrders() {
		List<PoplarOrder> orderlist = this.getOrderService().getAllOrderList();
		// 坐席下单后24小时未处理的处理
		for (PoplarOrder OO : orderlist) {
			if ("".equals(OO.getAccpectDate()) || OO.getAccpectDate() == null) {
				Date orderTime = OO.getRecordTime();
				long timzs = (new Date().getTime() - orderTime.getTime()) / 1000 / 60 / 60;
				if (timzs > 24) {
					OO.setCompleDate(new Date());
					OO.setProcessSucess("2");
					OO.setAccpectDate(new Date());
					OO.setAudState(2);
					BusinessOrder bo = new BusinessOrder();
					bo.setTransationSatet("未成交");
					bo.setBusRemark("坐席下单后该订单超过24小时商家未进行接单处理,系统自动处理!");
					this.getBusinessOrderService().save(bo);
					OO.setBusinessOrder(bo);
					this.getOrderService().saveOrupdate(OO);
				}
			} else {
				// 不为空情况
				if(!"".equals(OO.getBusinessOrder())&& OO.getBusinessOrder()!=null){
					if ("进行中".equals(OO.getBusinessOrder().getTransationSatet())) {
						Date orderTime = OO.getRecordTime();
						long timzs = (new Date().getTime() - orderTime.getTime()) / 1000 / 60 / 60;
						if (timzs > 24) {
							OO.setCompleDate(new Date());
							OO.setProcessSucess("2");
							
							OO.setAudState(2);
							BusinessOrder bo = this.getBusinessOrderService().get(
									OO.getBusinessOrder().getId());
							bo.setTransationSatet("未成交");
							bo.setBusRemark("坐席下单后24小时内未处理进行中的订单,系统自动处理");
							this.getBusinessOrderService().save(bo);
							this.getOrderService().saveOrupdate(OO);
						}
					}
				}
				if(!"".equals(OO.getBusinessOrder())&& OO.getBusinessOrder()!=null){
				if ("考虑中".equals(OO.getBusinessOrder().getTransationSatet())) {
					Date orderTime = OO.getRecordTime();
					long timzs = (new Date().getTime() - orderTime.getTime()) / 1000 / 60 / 60;
					if (timzs > 24) {
						OO.setCompleDate(new Date());
						OO.setProcessSucess("2");
					
						OO.setAudState(2);
						BusinessOrder bo = this.getBusinessOrderService().get(
								OO.getBusinessOrder().getId());
						bo.setTransationSatet("未成交");
						bo.setBusRemark("坐席下单后24小时内未处理考虑中的订单,系统自动处理");
						this.getBusinessOrderService().save(bo);
						this.getOrderService().saveOrupdate(OO);
					}
				}
				}
				if(!"".equals(OO.getBusinessOrder())&& OO.getBusinessOrder()!=null){
				if ("预约".equals(OO.getBusinessOrder().getTransationSatet())) {

					Date reserveTime = OO.getBusinessOrder().getRserveTime();
					long timzs = (reserveTime.getTime() - new Date().getTime()) / 1000 / 60 / 60;
					if (timzs < 2 && timzs > 0) {
						// 需要提醒
					} else if (timzs < -240) {
						OO.setCompleDate(OO.getBusinessOrder().getRserveTime());
						OO.setProcessSucess("2");
					
						OO.setAudState(2);
						BusinessOrder bo = this.getBusinessOrderService().get(
								OO.getBusinessOrder().getId());
						bo.setTransationSatet("未成交");
						bo.setBusRemark("坐席下单后24小时未对预约的订单进行处理,系统自动处理");
						this.getBusinessOrderService().save(bo);
						this.getOrderService().saveOrupdate(OO);
					}
				}
				}
			}
		}

	}
}
